package basic;

import java.awt.Point;

/**
 * This class defines a viewing grid where entity are placed. Each entity is
 * assigned to one specific cell. The grid and cells dimensions are variable.
 */
public class GridSVG {

	/** The width. */
	private int width = 0;

	/** The height. */
	private int heigth = 0;

	/** The cell_width. */
	private int cell_width = 0;

	/** The cell_heigth. */
	private int cell_heigth = 0;

	/** The table. */
	private boolean[][] table = null;

	/**
	 * Instantiates a new grid svg.
	 */
	public GridSVG() {
		super();
	}

	/**
	 * Sets the grid svg from dim table.
	 * 
	 * @param cell_width
	 *            the cell_width
	 * @param cell_heigth
	 *            the cell_heigth
	 * @param nrow
	 *            the nrow
	 * @param ncol
	 *            the ncol
	 */
	public void setGridSVGFromDimTable(int cell_width, int cell_heigth,
			int nrow, int ncol) {
		this.cell_width = cell_width;
		this.cell_heigth = cell_heigth;
		this.width = this.cell_width * ncol;
		this.heigth = this.cell_heigth * nrow;
		this.table = new boolean[nrow][ncol];
		for (int i = 0; i < this.table.length; i++)
			for (int j = 0; j < this.table[i].length; j++)
				this.table[i][j] = true;
	}

	/**
	 * Sets the grid svg from length.
	 * 
	 * @param cell_width
	 *            the cell_width
	 * @param cell_heigth
	 *            the cell_heigth
	 * @param width
	 *            the width
	 * @param heigth
	 *            the height
	 */
	public void setGridSVGFromLength(int cell_width, int cell_heigth,
			int width, int heigth) {
		this.cell_width = cell_width;
		this.cell_heigth = cell_heigth;
		this.width = width;
		this.heigth = heigth;

		int ncol = (int) (this.width / this.cell_width);
		int nrow = (int) (this.heigth / this.cell_heigth);

		this.table = new boolean[nrow][ncol];
		for (int i = 0; i < this.table.length; i++)
			for (int j = 0; j < this.table[i].length; j++)
				this.table[i][j] = true;
	}

	/**
	 * Gets the grid columns.
	 * 
	 * @return the columns
	 */
	public int getCols() {
		return (int) (this.width / this.cell_width);
	}

	/**
	 * Gets the rows.
	 * 
	 * @return the rows
	 */
	public int getRows() {
		return (int) (this.heigth / this.cell_heigth);
	}

	/**
	 * Gets the width.
	 * 
	 * @return the width
	 */
	public int getWidth() {
		return this.width;
	}

	/**
	 * Gets the height.
	 * 
	 * @return the height
	 */
	public int getHeigth() {
		return this.heigth;
	}

	/**
	 * Sets the width.
	 * 
	 * @param width
	 *            the new width
	 */
	public void setWidth(int width) {
		this.width = width;
	}

	/**
	 * Sets the height.
	 * 
	 * @param heigth
	 *            the new height
	 */
	public void setHeigth(int heigth) {
		this.heigth = heigth;
	}

	/**
	 * Gets the cell width.
	 * 
	 * @return the cell width
	 */
	public int getCellWidth() {
		return this.cell_width;
	}

	/**
	 * Gets the cell height.
	 * 
	 * @return the cell height
	 */
	public int getCellHeigth() {
		return this.cell_heigth;
	}

	/**
	 * Sets the cell width.
	 * 
	 * @param width
	 *            the new cell width
	 */
	public void setCellWidth(int width) {
		this.cell_width = width;
	}

	/**
	 * Sets the cell height.
	 * 
	 * @param heigth
	 *            the new cell height
	 */
	public void setCellHeigth(int heigth) {
		this.cell_heigth = heigth;
	}

	/**
	 * Returns x/y cell coordinates accord to cell' index identifier.
	 * 
	 * @param indexr
	 *            the cell row index
	 * @param indexc
	 *            the cell column index
	 * @return the cell coordinates
	 */
	public Point getCellCoordinates(int indexr, int indexc) {
		int x = 0;
		int y = 0;

		if (indexr == 0)
			y = this.cell_heigth;
		else
			y = (indexr + 1) * this.cell_heigth;
		if (indexc == 0)
			x = this.cell_width;
		else
			x = (indexc + 1) * this.cell_width;

		Point temp = new Point();
		temp.setLocation(x, y);
		return temp;
	}

	/**
	 * Sets entity grid coordinates if grid's cell is empty.
	 * 
	 * @param ent
	 *            the entity
	 * @param cols
	 *            the columns
	 * @param rows
	 *            the rows
	 * @return true, if successful
	 */
	public boolean setNewFreeCellRandom(Entity ent, int cols, int rows) {
		if (this.table[rows][cols]) {
			ent.setY(rows);
			ent.setX(cols);
			this.table[rows][cols] = false;
			return true;
		}
		return false;
	}

	/**
	 * Checks if grid is fill;.
	 * 
	 * @return true, if is all cells fill
	 */
	public boolean isAllCellsFill() {
		for (int i = 0; i < this.table.length; i++) {
			for (int j = 0; j < this.table[i].length; j++) {
				if (this.table[i][j])
					return false;
			}
		}
		return true;
	}

	/**
	 * Empty grid.
	 */
	public void initialize() {
		for (int i = 0; i < this.table.length; i++)
			for (int j = 0; j < this.table[i].length; j++)
				this.table[i][j] = true;
	}

	/**
	 * Sets the next free cell.
	 * 
	 * @param ent
	 *            the new next free cell
	 */
	public void setNextFreeCell(Entity ent) {
		boolean exit = false;
		for (int i = 0; i < this.table.length; i++) {
			for (int j = 0; j < this.table[i].length; j++) {
				if (this.table[i][j]) {
					ent.setY(i);
					ent.setX(j);
					this.table[i][j] = false;
					exit = true;
					break;
				}
			}
			if (exit)
				break;
		}
	}
}
